package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.dto.OrderDto;
import com.itheima.entity.OrderDetail;
import com.itheima.entity.Orders;
import com.itheima.entity.User;
import com.itheima.service.OrderDetailService;
import com.itheima.service.OrdersService;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private UserService userService;
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return R.success("支付成功");
    }
    @GetMapping("/userPage")
    public R<Page<OrderDto>> getOrders(Integer page, Integer pageSize){
        Long userId = BaseContext.getId();
        Page<Orders> pageInit=new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Orders::getUserId,userId);
        pageInit = ordersService.page(pageInit, lqw);
        Page<OrderDto> orderDtoPage=new Page<>();
        BeanUtils.copyProperties(pageInit,orderDtoPage,"records");
        List<Orders> records = pageInit.getRecords();
        List<OrderDto> collect = records.stream().map(item -> {
            OrderDto orderDto = new OrderDto();
            BeanUtils.copyProperties(item, orderDto);
            LambdaQueryWrapper<OrderDetail> qw = new LambdaQueryWrapper<>();
            qw.eq(OrderDetail::getOrderId, item.getId());
            List<OrderDetail> list = orderDetailService.list(qw);
            orderDto.setOrderDetails(list);
            orderDto.setSumNum(list.size());
            return orderDto;
        }).collect(Collectors.toList());
        orderDtoPage.setRecords(collect);
        return R.success(orderDtoPage);
    }
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, Long number, String beginTime,String endTime){
//        log.info("当前页码为:"+page);
//        log.info("页数为:"+pageSize);
//        log.info("订单号为:"+number);
//        log.info("开始时间为:"+beginTime);
//        log.info("结束时间为:"+endTime);
        Page<Orders> pageInit=new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> lqw=new LambdaQueryWrapper<>();
        if(number!=null){
            lqw.eq(Orders::getNumber,number);
        }
        LocalDateTime begin=null;
        LocalDateTime end=null;
        if(beginTime!=null){
            DateTimeFormatter fmt=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            begin = LocalDateTime.parse(beginTime, fmt);
            end = LocalDateTime.parse(endTime, fmt);
            lqw.gt(Orders::getCheckoutTime,begin).lt(Orders::getCheckoutTime,end);
        }
        Page<Orders> page1 = ordersService.page(pageInit, lqw);
        List<Orders> ordersList = page1.getRecords();
        List<Orders> collect = ordersList.stream().map(item -> {
            Long userId = item.getUserId();
            User user = userService.getById(userId);
            item.setUserName(user.getName());
            return item;
        }).collect(Collectors.toList());
        page1.setRecords(collect);
        return R.success(page1);
    }
    @PutMapping()
    public R<String> editOrder(@RequestBody Orders orders){
        Orders orders1 = ordersService.getById(orders.getId());
        orders1.setStatus(orders.getStatus());
        ordersService.updateById(orders1);
        return  R.success("更改订单状态成功");
    }
}
